package com.mfangsoft.zhuangjialong.common.utils;

import java.io.UnsupportedEncodingException;
import java.security.AlgorithmParameters;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.Security;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.bouncycastle.jce.provider.BouncyCastleProvider;


public class AESUtil {
    public static final AESUtil INSTANCE = new AESUtil();

    public static boolean initialized = false;

    /**
     * AES解密
     * @param content 密文
     * @return
     * @throws InvalidAlgorithmParameterException
     * @throws NoSuchProviderException
     */
    public byte[] decrypt(byte[] content, byte[] keyByte, byte[] ivByte) throws InvalidAlgorithmParameterException {
        initialize();
        try {
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
            Key sKeySpec = new SecretKeySpec(keyByte, "AES");

            cipher.init(Cipher.DECRYPT_MODE, sKeySpec, generateIV(ivByte));// 初始化
            byte[] result = cipher.doFinal(content);
            return result;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            e.printStackTrace();
        } catch (BadPaddingException e) {
            e.printStackTrace();
        } catch (NoSuchProviderException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    public static void initialize(){
        if (initialized) return;
        Security.addProvider(new BouncyCastleProvider());
        initialized = true;
    }
    //生成iv
    public static AlgorithmParameters generateIV(byte[] iv) throws Exception{
        AlgorithmParameters params = AlgorithmParameters.getInstance("AES");
        params.init(new IvParameterSpec(iv));
        return params;
    }
    
    /**
     * 解密用户敏感数据
     */
    public static void main(String[] args) {
    	 String encryptedData = "SYQ1qMAiGj4lg+Z9DCodXNsfdLGTwE2p09CQTDRObdQe/kPt6g4wG0q8ClcaSVzErtIuzdJzsjYIvynIAOF6+aDGXMt3Kwum8G/CWbPC60wcJEgLyshkKVBOMBv6ZQYKkKJOvOqaruvQwPtNstCjY+l2aiQpacDvNUOidL/V0f/wHVu0zY8gSxDK4Q/6KGRS21FJL0A33RPgA7lB47xa4VtKG9VyRcu5XoSGbr1G5AciTT4EmHO0NarllY/E9OTNYUifTmjpWqF64xBvrPMayaemJaxdehuH8IOHl+IUeHhj1TmRm0lwz6fAoHevD98PWbQhvqYbHXqsied+yxha0c/MhlhuSP6g/rKfZm+tO/Plp5o5rzYG5N1ka8Yftk20aTYvNMHpYAQ18rn2ohkCrrqgZDj+Hm5gF/GLpBCAqjgGcVVgIZNp7N5tVK4naGH6xz8yg5m6bRES+jCEtAkzY/d52+E0lrDMgjpENuJYx7X+ArNJqPXk+AHZQCsj6HCpPp0Fai9qJtOovIc/23i/GA==";
         String iv = "3OsjDFTX3KM1TXDJWiKp8g==";
         String session_key = "413wC0KCrUs3r+JkaB8jqw==";
         try {
             byte[] resultByte = AESUtil.INSTANCE.decrypt(Base64.decode(encryptedData), Base64.decode(session_key), Base64.decode(iv));
             if(null != resultByte && resultByte.length > 0){
                 String userInfo = new String(resultByte, "UTF-8");
                 System.out.println(userInfo);
             }
         } catch (InvalidAlgorithmParameterException e) {
             e.printStackTrace();
         } catch (UnsupportedEncodingException e) {
             e.printStackTrace();
         }
	}
}